package com.shenzhen.horselive.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.SumBean;
import com.shenzhen.horselive.beans.UserAcountBean;
import com.shenzhen.horselive.databinding.ActivityHongBaoGuValueBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;
import com.shenzhen.horselive.presenter.ViewObserver;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 红包股价值
 */
public class HongBaoGuValueActivity extends AppBarActivity {

    ActivityHongBaoGuValueBinding  binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityHongBaoGuValueBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initViews();
        setStyleRed();
        setTitle("红包股价值");
        setMenuText("明细");

    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initViews() {
        //转出
        binding.btCashOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FenZhiTransferOutActivity.start(HongBaoGuValueActivity.this,2);
            }
        });
        //
        binding.btDui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //2.0做
                EmptyActivity.start(HongBaoGuValueActivity.this,"红包股兑换");
            }
        });

    }


    @Override
    protected void onActionMenuViewClick() {
        super.onActionMenuViewClick();
        FenZhiDetailsActivity.start(this,2);
    }

    private void initData() {
        //获取账户信息
        getAcountForType(2);
        //获取今日收益
        getAcountSumForType(2);
    }


    //帐户类型: 0:人民币帐户 ,1:蜂值帐户 ,2:红包股帐户 ,3:金卡帐户
    private void getAcountForType(int i) {
        ApiFactory.getApiService().accountTypes(User.get().getMyInfo().getToken(),i+"")
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<UserAcountBean>(this) {
                    @Override
                    protected void onNextInActive(UserAcountBean userAcountBean) {
                       if (userAcountBean.isSuccess())
                       {
                           //最多保留十位
                           NumberFormat  format = NumberFormat.getInstance();
                           format.setMaximumFractionDigits(10);
                           binding.tvValue.setText("¥"+format.format(Double.parseDouble(userAcountBean.getData().getValue())));
                           binding.tvNum.setText(format.format(Double.parseDouble(userAcountBean.getData().getAmount())));
//                           binding.tvPrice.setText(userAcountBean.getData().getRatio());
                       }
                    }
                });

    }

    /**
     * 获取收益
     * @param i
     */
    //帐户类型: 0:人民币帐户 ,1:蜂值帐户 ,2:红包股帐户 ,3:金卡帐户
    private void getAcountSumForType(int i) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        String  staredtime =  sf.format(c.getTime());
        c.add(Calendar.DAY_OF_MONTH, 1);
        String  endtime =  sf.format(c.getTime());
        ApiFactory.getApiService().accountTypesSum(User.get().getMyInfo().getToken()
                ,i+"","1",staredtime,endtime)
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new ViewObserver<SumBean>(this) {
                    @Override
                    protected void onNextInActive(SumBean sumBean) {
                        if (sumBean.isSuccess())
                        {

                            //今日收益
                            NumberFormat  format   =  NumberFormat.getInstance();
                            format.setMaximumFractionDigits(10);
                            binding.tvPrice.setText(format.format(Double.parseDouble(sumBean.getData().getSumAmount())));
                        }
                    }
                });

    }







}